﻿using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace blackhouse.WebNemo.Business.Translate {

    public sealed class Translator {

        #region Fields

        private IGroup group;
        private CultureInfo defaultLanguage;
        private ITranslate translateSupport;
        private RecordExtension groupRecordExtension;

        #endregion

        #region Constructors

        public Translator(IGroup group, CultureInfo defaultLanguage) {
            this.group = group;
            this.defaultLanguage = defaultLanguage;
            this.translateSupport = Factory.Instance().GetTranslates();
            this.groupRecordExtension = RecordExtension.GetRecordExtension(this.group);
        }

        #endregion

        #region Methods

        public string GetText(CultureInfo language, int columnId, int recordId) {
            ITranslate translate = this.translateSupport.GetTranslate(this.group.Id, columnId, recordId);
            return
                language.Equals(this.defaultLanguage) ?
                translate.TranslationOryginal() :
                translate.Translation(language);
        }

        public void SetText(CultureInfo language, int columnId, int recordId, string value) {
            string prevValue = this.GetText(language,columnId,recordId);
            ITranslate translate = this.translateSupport.GetTranslate(this.group.Id, columnId, recordId);
            translate.SetTranslation(language, value);
            this.groupRecordExtension.RecordTranslated(language, columnId, recordId, prevValue, value);
        }

        #endregion

    }
}
